<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }
        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }
    </style>
    <script>
        var pageNum = getQueryVariable("pageNum");
        $(function () {
            loadRole();
		$('#backid').click(function(){
				window.location.href="/User/index.html?pageNum=" + pageNum;
		 });
        $('#addBtn').click(function(){
            // alert($("#addUserForm").serialize());
            $.ajax({
                url:"/addUser",
                type:"POST",
                data:$("#addUserForm").serialize(),
                dataType:"JSON",
                success:function (vo) {
                    if (vo.code == 200){
                        alert("新增用户成功！");
                        window.location.href = "/User/index.html?pageNum=" + pageNum;
                    } else{
                        alert(vo.message);
                    }
                }
            })
        });
    });
    //加载角色数据
    function loadRole() {
        $.ajax({
            url:"/rolelist",
            type: 'GET',
            data:"",
            dataType:'JSON',
            async:false,
            success:function (vo){
                let roles = vo.objList;
                let options = "";
                for (let i = 0; i < roles.length; i++) {
                    let role = roles[i];
                    // let selected = roleid == role.roleid?"selected=\'selected\'":"";
                    let selected = "";
                    options += "<option value=" + "\'" + role.roleid + "\'" + selected + ">" + role.rolename + "</option>"
                }
                $("#roleSelect").html(options);
            }
        })
    }
    //从url里获取参数，window.location.search.search从问号(?)开始的 URL（查询部分）
    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return false;
    }
</script>
</head>
<body>
<form id="addUserForm" class="definewidth m20">
<input type="hidden" name="id" value="{$user.id}" />
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">登录名</td>
            <td><input type="text" name="username"/></td>
        </tr>
        <tr>
            <td class="tableleft">密码</td>
            <td><input type="password" name="password"/></td>
        </tr>
        <tr>
            <td class="tableleft">真实姓名</td>
            <td><input type="text" name="realname"/></td>
        </tr>
        <tr>
            <td class="tableleft">邮箱</td>
            <td><input type="text" name="email"/></td>
        </tr>
        <tr>
            <td class="tableleft">状态</td>
            <td>
                <input type="radio" name="zt" value="1" checked/> 启用
              <input type="radio" name="zt" value="0" /> 禁用
            </td>
        </tr>
        <tr>
            <td class="tableleft">角色</td>
            <td>
            	<select name="roleid" id="roleSelect">
       			 </select>
        	</td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button id="addBtn" class="btn btn-primary" type="button">添加</button>&nbsp;&nbsp;
                <button id="backid" class="btn btn-success" name="backid" >返回列表</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>